package cn.acewill.yxd.yunxundian.componet;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.model.CommonMapBean;
import cn.acewill.yxd.yunxundian.model.EmailBean;
import cn.acewill.yxd.yunxundian.model.RMain;
import cn.acewill.yxd.yunxundian.model.reportHeadBean;
import cn.acewill.yxd.yunxundian.model.reportTailBean;
import cn.acewill.yxd.yunxundian.model.tReportHead;
import cn.acewill.yxd.yunxundian.model.tReportTail;
import cn.acewill.yxd.yunxundian.model.tempLostRR;
import cn.acewill.yxd.yunxundian.pubClass.RMainUtils;
import cn.acewill.yxd.yunxundian.ui.MainActivity;
import cn.acewill.yxd.yunxundian.ui.sign.PreViewAcitivity;
import cn.acewill.yxd.yunxundian.ui.sign.SendMailActivity;

/**
 * Created by xxx on 2018/2/7.
 */

public class CheckDataDialog{

    protected YXDAPP yxdapp=new YXDAPP();
    protected DbManager db = x.getDb(yxdapp.getDaoConfig());

    private Context mContext;
    private  CheckDataDialog (Context context){
        this.mContext = context;

    }
    private static CheckDataDialog instance;

    public static CheckDataDialog getInstance(Context context){
        if (instance == null){
            instance = new CheckDataDialog(context.getApplicationContext());
        }
        return instance;
    }

    /**
     * 判断数据是否可提交
     * @param schId 日程id
     */
    public boolean[] checkDataComplete(int schId,List<RMain> rMains){
        boolean NEED_FOLLOW = false;
        boolean NEED_SHOP_SIGN = false;
        boolean NEED_JIBENDATA = false;
        if (rMains==null){
            return new boolean[]{NEED_FOLLOW,NEED_SHOP_SIGN,NEED_JIBENDATA};
        }
        try {
            List<tempLostRR> rrList = db.selector(tempLostRR.class).where("schId", "=", schId)
                    .and("empId", "=", MainActivity.id).findAll();
            if (rrList!=null&&rrList.size()>0){
                List<tempLostRR> list = excludeSameRR(rrList,rMains);//满分及重复项
                if (list!=null&&list.size()>0) {
                    for (tempLostRR rr:list) {
                        if (rr.isFbIsCheck()) {
                            if (TextUtils.isEmpty(rr.getXiongdongTime())||TextUtils.isEmpty(rr.getXindongId()+"")) {
                                NEED_FOLLOW = true;
                                break;
                            }
                        }
                        if (!rr.isFbIsCheck()) {
                            NEED_FOLLOW = true;
                        }
                    }
                }
            }
            //判断是否门店签字
            CommonMapBean cmb = db.selector(CommonMapBean.class).where("CmType", "=", "门店签名")
                    .and("IndexNum", "=", schId + "")
                    .findFirst();
            if (cmb != null) {
                if (!cmb.getCmValue().equals("")) {
                    NEED_SHOP_SIGN = false;
                }else{
                    NEED_SHOP_SIGN = true;
                }
            }else{
                NEED_SHOP_SIGN = true;
            }
//            showPointDialog();

            //判断基本信息是否填写,
            NEED_JIBENDATA = chekJibenData(rMains,schId);
        } catch (DbException e) {
            e.printStackTrace();
        }
        return new boolean[]{NEED_FOLLOW,NEED_SHOP_SIGN,NEED_JIBENDATA};
    }
    public boolean[] checkDataComplete(int schId){
        boolean NEED_FOLLOW = false;
        boolean NEED_SHOP_SIGN = false;
        Boolean NEED_JIBENDATA = false;
        List<RMain> rMains = RMainUtils.getInstance().loadRMins(schId);
        if (rMains==null){
            return null;
        }
        try {
            List<tempLostRR> rrList = db.selector(tempLostRR.class).where("schId", "=", schId)
                    .and("empId", "=", MainActivity.id).findAll();
            if (rrList!=null&&rrList.size()>0){

                List<tempLostRR> list = excludeSameRR(rrList,rMains);//满分及重复项
                if (list!=null&&list.size()>0) {
                    for (tempLostRR rr:list) {
                        if (rr.isFbIsCheck()) {
                            if (TextUtils.isEmpty(rr.getXiongdongTime())||TextUtils.isEmpty(rr.getXindongId()+"")) {
                                NEED_FOLLOW = true;
                                break;
                            }
                        }
                        if (!rr.isFbIsCheck()) {
                            NEED_FOLLOW = true;
                        }
                    }
                }
            }
            //判断是否门店签字
            CommonMapBean cmb = db.selector(CommonMapBean.class).where("CmType", "=", "门店签名")
                    .and("IndexNum", "=", schId + "")
                    .findFirst();
            if (cmb != null) {
                if (!cmb.getCmValue().equals("")) {
                    NEED_SHOP_SIGN = false;
                }else{
                    NEED_SHOP_SIGN = true;
                }
            }else{
                NEED_SHOP_SIGN = true;
            }
//            showPointDialog();

        } catch (DbException e) {
            e.printStackTrace();
        }
        NEED_JIBENDATA = chekJibenData(rMains,schId);
        return new boolean[]{NEED_FOLLOW,NEED_SHOP_SIGN,NEED_JIBENDATA};
    }

    public void showTipDialog( final int schId, final onDialogClickListener dialogClickListener){
        boolean need[] = checkDataComplete(schId);
        if (!need[0]&&!need[1]&&!need[2]){
            mContext.startActivity(new Intent(mContext,SendMailActivity.class).putExtra("schId",schId));
        }else{
            final Dialog dialog= new Dialog(mContext,R.style.mydialog);
            View view = LayoutInflater.from(mContext).inflate(R.layout.eva_check_dialog, null,false);
            TextView addFollow = view.findViewById(R.id.addFollow);
            TextView shopSign = view.findViewById(R.id.shopSign);
            TextView jiBenData = view.findViewById(R.id.jiBenData);
            TextView gotoSubmit = view.findViewById(R.id.gotoSubmit);
            gotoSubmit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    dialogClickListener.onSubmitClick();

                }
            });
            TextView cancel_action = view.findViewById(R.id.cancel_action);
            cancel_action.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    dialogClickListener.onEditClick();
                }
            });
            if (!need[0]){
                addFollow.setVisibility(View.GONE);
            }
            if (!need[1]){
                shopSign.setVisibility(View.GONE);
            }
            if (!need[2]){
                jiBenData.setVisibility(View.GONE);
            }
            dialog.setContentView(view);
            dialog.setCancelable(true);
            dialog.show();
        }
    }

    public void showTipDialog(int schId, List<RMain>rMains, final onDialogClickListener dialogClickListener){
        boolean need[] = checkDataComplete(schId);
        if (!need[0]&&!need[1]&&!need[2]){
            mContext.startActivity(new Intent(mContext,SendMailActivity.class).putExtra("schId",schId));
        }else{
            final Dialog dialog= new Dialog(mContext,R.style.mydialog);
            View view = LayoutInflater.from(mContext).inflate(R.layout.eva_check_dialog, null,false);
            TextView addFollow = view.findViewById(R.id.addFollow);
            TextView shopSign = view.findViewById(R.id.shopSign);
            TextView jiBenData = view.findViewById(R.id.jiBenData);
            TextView gotoSubmit = view.findViewById(R.id.gotoSubmit);
            gotoSubmit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    dialogClickListener.onSubmitClick();

                }
            });
            TextView cancel_action = view.findViewById(R.id.cancel_action);
            cancel_action.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    dialogClickListener.onEditClick();
                }
            });
            if (!need[0]){
                addFollow.setVisibility(View.GONE);
            }
            if (!need[1]){
                shopSign.setVisibility(View.GONE);
            }
            if (!need[2]){
                jiBenData.setVisibility(View.GONE);
            }
            dialog.setContentView(view);
            dialog.setCancelable(true);
            dialog.show();
        }
    }


    //排除重复评估项
    public List<tempLostRR> excludeSameRR(List<tempLostRR> rrList, List<RMain>rMains){
        for  ( int i = 0; i<rrList.size()-1 ;i ++)  {
            for  ( int j = rrList.size() - 1 ;j > i;j --)  {
                if  (rrList.get(j).equals(rrList.get(i)))  {
                    rrList.remove(j);
                }
            }
        }
        return excludeFullMarkAndMinus(rrList,rMains);

    }
    //排除满分与分数为-1的情况
    private List<tempLostRR> excludeFullMarkAndMinus( List<tempLostRR> rrList,List<RMain> rMains){
        if (rrList!=null&&rrList.size()>0){
            for (int i =0;i<rrList.size();i++){
                if (rrList.get(i).getScore()==-1||rrList.get(i).getScore()== RMainUtils.getInstance().getMaxScoreByrowId(rMains,rrList.get(i).getRRowId())){
                    rrList.remove(i);
                    i = i-1;
                }
            }
        }
        return rrList;
    }

    private boolean chekJibenData(List<RMain> rMains,int schId){
        boolean NEED_JIBENDATA = true;
        //判断基本信息是否填写,
        int mainId = rMains.get(0).getQ().getID();
        List<reportHeadBean>  mheadList= null;
        try {
            mheadList = db.selector(reportHeadBean.class).where("reportMainID","=",mainId).orderBy("orderValue").findAll();
            List<reportTailBean>  mtailList=db.selector(reportTailBean.class).where("reportMainId","=",mainId).orderBy("orderValue").findAll();
            boolean headIsEdit = false;
            if (mheadList!=null&&mheadList.size()>0){
                for (reportHeadBean rh:mheadList){
                    tReportHead trh= db.selector(tReportHead.class).where("reportHeadId","=",rh.getID()).and("ID","=", schId).findFirst();
                    if (trh!=null){
                        headIsEdit = true;
                        NEED_JIBENDATA = false;
                        break;
                    }
                }
            }
            if (!headIsEdit){
                if (mtailList!=null&&mtailList.size()>0) {
                    for (reportTailBean rt : mtailList) {
                        tReportTail trt = db.selector(tReportTail.class).where("reportTailId", "=", rt.getID()).and("ID", "=", schId).findFirst();
                        if (trt != null) {
                            NEED_JIBENDATA = false;
                        }
                    }
                }
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        return NEED_JIBENDATA;

    }
    public interface onDialogClickListener{
        void onEditClick();
        void onSubmitClick();
    }

}
